<?php

namespace App\Admin\Controllers;

use App\Model\Goods;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Facades\URL;

class GoodsController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '商品';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Goods());

        $grid->column('id', __('Id'));
        $grid->column('goods_name', __('商品名称'));
        $grid->column('goods_image', __('商品主图'))->image('', 80, 80);
        $grid->column('min_price', __('最低价格(元)'));
        $grid->column('desc', __('商品描述'));
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('最后一次更改时间'));
        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            $filter->equal('goods_name','商品名称');

        });

        $grid->actions(function ($actions) {

            // 去掉查看
            $actions->disableView();
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Goods::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('goods_name', __('Goods name'));
        $show->field('goods_image', __('Goods image'));
        $show->field('min_price', __('Min price'));
        $show->field('desc', __('Desc'));
        $show->field('goods_desc_image', __('Goods desc image'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('goods_lunbo', __('Goods lunbo'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Goods());

        $form->text('goods_name', __('商品名称'))->required();
        $form->image('goods_image', __('商品图片'))->removable()->required();
        $form->number('min_price', __('最低价格'))->required();
        $form->textarea('desc', __('商品描述'))->required();
        $form->multipleImage('goods_desc_image', __('商品展示图'))->removable();
//        $form->multipleImage('goods_lunbo', __('商品轮播图'));
        $form->hasMany('goods_sku','商品sku', function (Form\NestedForm $form) {
            $form->text('sku_name','sku名称')->required();
            $form->image('sku_image','sku图片')->removable()->required();
            $form->number('price','sku价格')->required();
        });
        return $form;
    }
}
